<template>
  <div>
    <el-button @click="show_dialog">新增</el-button>
    <el-dialog :title="title" :visible.sync="dialog_hos" width="80%">
      <!-- {{form}} -->
      <el-form :model="form_hos">
        <el-form-item label="医院名称" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.name"></el-input>
        </el-form-item>

        <el-form-item label="医院简介" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.introduce"></el-input>
        </el-form-item>
        <el-form-item label="省市区" :label-width="formLabelWidth_hos">
          <el-cascader
            v-model="form_hos.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover',label:'name',value:'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.address"></el-input>
        </el-form-item>
        <el-form-item label="标记" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.flags"></el-input>
        </el-form-item>
        <el-form-item label="经度" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.longitude" disabled></el-input>
        </el-form-item>
        <el-form-item label="纬度" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.latitude" disabled></el-input>
        </el-form-item>
        <el-form-item label="选择经纬度" :label-width="formLabelWidth_hos">
          <el-button @click="show_map">打开地图</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hos = false">取 消</el-button>
        <el-button type="primary" @click="save_hos()">发 布</el-button>
      </div>
    </el-dialog>
    <el-dialog title="选择地点" :visible.sync="dialog_map" width="80%">
      <div id="Map" style="height:400px"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确定</el-button>
      </div>
    </el-dialog>
    <el-table :data="hos.list" style="width: 100%">
      <el-table-column align="center" prop="name" label="医院名称">
      </el-table-column>
      <el-table-column
        align="center"
        prop="introduce"
        label="医院简介"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="address"
        label="地址"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="flags"
        label="标记"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="longitude"
        label="经度"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="latitude"
        label="纬度"
      ></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button @click="edit_hos(scope.row)">修改</el-button>
          <el-button @click="delete_hos(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change"
      :total="hos.total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
import pca from '../../assets/pca.json'
export default {
  computed: {
    ...mapState("hos", ["hos"]),
  },
  data() {
    return {
      pca,
      obj: {
        page: 1,
        pageSize: 10,
      },
      dialog_hos: false,
      dialog_map:false,
      title: "",
      form_hos: {},
      formLabelWidth_hos: '80',
      map:{}
    };
  },
  created() {
    this.findHos(this.obj);
  },
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true, //表示深度监听
    },
  },
  methods: {
    initMap(){
      this.map = new AMap.Map("Map",{
        resizeEnable:true
      })
    },
    //获取经纬度
    showInfoClick(e){
      this.form_hos.longitude = e.lnglat.getLng();
      this.form_hos.latitude = e.lnglat.getLat();
    },
    //给地图绑定单击事件
    clickOn(){
        this.map.on('click',this.showInfoClick);
    },
    ...mapActions("hos", ["findHos","saveHos"]),
    edit_hos() {},
    delete_hos() {},
    show_map(){
      this.dialog_map = true;
      setTimeout(()=>{
        this.initMap();
        this.clickOn();
      },1000)
    },
    change(page) {
        this.obj.page = page;
    },
    //显示模态框
    show_dialog() {
      this.dialog_hos = true;
    },
    handleChange(){},
    save_hos(){
        this.form_hos.province = this.form_hos.pca[0];
        this.form_hos.city = this.form_hos.pca[1];
        this.form_hos.area = this.form_hos.pca[2];
        this.saveHos(this.form_hos).then((res)=>{
            this.dialog_hos = false;
            this.findHos(this.obj);
        })
    }
  },
};
</script>